package com.ildepinc.persistence.concretes;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;

public class Utils {
  public static Timestamp addDaysToTimestamp(Timestamp startDate, Integer daysToAdd) {
    Timestamp endDate = null;
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(startDate.getTime());
    cal.add(Calendar.DAY_OF_MONTH, daysToAdd);
    endDate = new Timestamp(cal.getTime().getTime());
    return endDate;
  }
  // Two wrapper methods to bypass the behaviour of getInt and
  // getFloat methods in the java.sql.ResultSet class
  // https://nicolasbouliane.com/blog/how-to-get-an-integer-from-a-java-resultset
  public Integer getIntegerNull(ResultSet rs, String dbField) throws SQLException {
    Integer result = null;
    result = rs.getInt(dbField);
    result = rs.wasNull() ? null : result;
    return result;
  }
  public Float getFloatNull(ResultSet rs, String dbField) throws SQLException {
    Float result = null;
    result = rs.getFloat(dbField);
    result = rs.wasNull() ? null : result;
    return result;
  }
}
